package com.qf.controller;

import com.netflix.discovery.EurekaClient;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import com.qf.feign.GoodsFeign;
import com.qf.pojo.Goods;
import com.qf.service.TestCacheService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

/**
 * spring为我们提供了很多模板对象
 *  RabbitMqTemplate
 *  JdbcTemplate
 *  RedisTemplate
 *  StringRedisTemplate
 *  RestTemplate
 * @author 千峰教育
 * @Company http://www.mobiletrain.org/
 * @Version 1.0
 */
@RestController
@RequestMapping("/order")
public class OrderController {

    @Autowired
    RestTemplate template;

    @Autowired
    EurekaClient eurekaClient;

    @Autowired
    GoodsFeign goodsFeign;

    @Autowired
    TestCacheService testCacheService;

    @RequestMapping("/testCache")
    public void testCache(){
        System.out.println(testCacheService.testCache(1));
        System.out.println(testCacheService.testCache(1));
        System.out.println(testCacheService.testCache(1));
        testCacheService.clearCache(1);
        System.out.println(testCacheService.testCache(1));
        System.out.println(testCacheService.testCache(1));
        System.out.println(testCacheService.testCache(1));
    }


    @RequestMapping("/index")
    public String index(){

//        InstanceInfo instanceInfo = eurekaClient.getNextServerFromEureka("QF-GOODS", false);
//        String url = instanceInfo.getHomePageUrl(); // url = SUN:9001

        //远程调用商品模块的控制器
//        String url = "http://QF-GOODS/index";
        /**
         * getForEntity: 模拟 http的get请求
         * 参数1： url路径
         * 参数2： 响应数据类型
         * 返回值： 响应实体
         */
//        ResponseEntity<String> responseEntity = template.getForEntity(url + "/index", String.class);
//        String body = responseEntity.getBody();
//        System.out.println("响应体：" + body);
//        HttpStatus statusCode = responseEntity.getStatusCode();
//        System.out.println(statusCode.is2xxSuccessful() ? "成功" : "失败");
//        System.out.println(statusCode.value());

        String body = goodsFeign.index();

        return "获取订单信息" + body;
    }

    @HystrixCommand(fallbackMethod = "findByIdFallback" ,commandProperties = {
            @HystrixProperty(name = "execution.isolation.strategy", value = "THREAD"),
            @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "1000")
    })
    @GetMapping("/findById")
    public Goods findById(Integer id){
        return  goodsFeign.findById2(id);
    }


    public Goods findByIdFallback(Integer id){
        System.out.println("服务降级了");
        return new Goods(id,  "服务降级了");
    }


    @GetMapping("/save")
    public String saveGoods(Goods goods){
        System.out.println("qf-order ------------------");
        return goodsFeign.save(goods);
    }
}
